package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    CartService cartService = new CartServiceImpl();

    /**
     *  按照条件查询购物车记录列表
     * @param request
     * @return
     * @throws Exception
     */
   public Object listCart(HttpServletRequest request) throws Exception{
       String businessIdStr = request.getParameter("businessId");
       Integer businessId = null;
       if(businessIdStr!=null){
            businessId = Integer.valueOf(businessIdStr);
       }
       String userId = request.getParameter("userId");
       //创建参数cart对象
       Cart cart = new Cart();
       cart.setBusinessId(businessId);
       cart.setUserId(userId);
       List<Cart> list = cartService.listCart(cart);
       return list;
   }


    /**
     *  添加一条购物车记录
     * @param request
     * @return
     * @throws Exception
     */
   public Object saveCart(HttpServletRequest request) throws Exception{
       Integer businessId = Integer.valueOf(request.getParameter("businessId"));
       String userId = request.getParameter("userId");
       Integer foodId = Integer.valueOf(request.getParameter("foodId"));
       //构造Cart参数
        Cart cart = new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        int n = cartService.saveCart(cart);
        return n;
   }

    /**
     *  按照条件删除一条购物车记录
     * @param request
     * @return
     * @throws Exception
     */
   public Object removeCart(HttpServletRequest request) throws Exception{
       Integer businessId = Integer.valueOf(request.getParameter("businessId"));
       String userId = request.getParameter("userId");
       Integer foodId = Integer.valueOf(request.getParameter("foodId"));
       //构造Cart参数
       Cart cart = new Cart();
       cart.setBusinessId(businessId);
       cart.setUserId(userId);
       cart.setFoodId(foodId);
       int n = cartService.removeCart(cart);
       return n;
   }

    /**
     *  按照条件修改购物记录中食物的数量
     * @param request
     * @return
     * @throws Exception
     */
   public Object updateCart(HttpServletRequest request) throws Exception{
       Integer businessId = Integer.valueOf(request.getParameter("businessId"));
       String userId = request.getParameter("userId");
       Integer foodId = Integer.valueOf(request.getParameter("foodId"));
       Integer quantity = Integer.valueOf(request.getParameter("quantity"));
       //构造Cart参数
       Cart cart = new Cart();
       cart.setBusinessId(businessId);
       cart.setUserId(userId);
       cart.setFoodId(foodId);
       cart.setQuantity(quantity);
       int n = cartService.updateCart(cart);
       return n;
   }
}
